package com.songqintuan.mall.customer;

import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import com.songqintuan.common.util.DESPlusUtil;
import com.songqintuan.common.util.MessageFactory;
import com.songqintuan.common.util.SpringFacesUtil;
import com.songqintuan.mall.constant.MallConstants;
import com.songqintuan.mall.constant.MallManageBeanConstants;
import com.songqintuan.mall.constant.MallSpringBeanConstants;
import com.songqintuan.mall.controller.LoginController;
import com.songqintuan.mall.util.CustomerUtil;
import com.songqintuan.model.po.Customer;
import com.songqintuan.persist.service.hibernate.ICustomerService;

@ManagedBean(name="updateCustomerPasswordPage")
@RequestScoped
public class UpdateCustomerPasswordPageBean implements java.io.Serializable {
	private static final long serialVersionUID = -8106629465344224720L;
	private String oldPassword;
	private String newPassword;
	private String verifyNewPassword;
	private List<String> errorMessages = new ArrayList<String>();
	
	public String getOldPassword() {
		return oldPassword;
	}
	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}
	public String getNewPassword() {
		return newPassword;
	}
	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}
	public String getVerifyNewPassword() {
		return verifyNewPassword;
	}
	public void setVerifyNewPassword(String verifyNewPassword) {
		this.verifyNewPassword = verifyNewPassword;
	}
	
	public List<String> getErrorMessages() {
		return errorMessages;
	}
	public void setErrorMessages(List<String> errorMessages) {
		this.errorMessages = errorMessages;
	}
	public UpdateCustomerPasswordPageBean(){
		
	}
	public String updatePassword(){
		this.getErrorMessages().clear();
		if (!(LoginController.loggedIn())) {
			return MallConstants.GO_TO_LOGIN;
		}
		ICustomerService<Customer,String> customerService = (ICustomerService<Customer,String>) SpringFacesUtil.getSpringBean(MallSpringBeanConstants.CUSTOMER_SERVICE_BEAN_NAME);
		customerService.setEntityClass(Customer.class);
		customerService.setPKClass(String.class);
		CurrentCustomerInfoBean currentCustomer = (CurrentCustomerInfoBean)SpringFacesUtil.getManagedBean(MallManageBeanConstants.CURRENT_CUSTOMER_BEAN_NAME);
		String loginName = currentCustomer.getCustomer().getLoginName();
		Customer customer = customerService.findUniqueBy("loginName", loginName);
		
		if (null == customer) {
			this.getErrorMessages().add(MessageFactory.getMessage(MallConstants.USER_BUNDLE_NAME,"loginname.notexist.error.message", FacesMessage.SEVERITY_ERROR, loginName).getDetail());
			return MallConstants.GO_TO_LOGIN;
		}
		String realOldPassword = (String) currentCustomer.getCustomer().getLoginPassword();
		String oldPassword = (String) this.getOldPassword();
		oldPassword = DESPlusUtil.encrypt(oldPassword);
		this.setOldPassword(oldPassword);
		if (!(realOldPassword.equals(oldPassword))) {
			this.getErrorMessages().add(MessageFactory.getMessage(MallConstants.USER_BUNDLE_NAME,"old.password.not.patch.error.message", FacesMessage.SEVERITY_ERROR).getDetail());
		}
		String newpassword = (String) this.getNewPassword();
		newpassword = DESPlusUtil.encrypt(newpassword);
		this.setNewPassword(newpassword);
		String verifyNewPassword = (String) this.getVerifyNewPassword();
		verifyNewPassword = DESPlusUtil.encrypt(verifyNewPassword);
		this.setVerifyNewPassword(verifyNewPassword);
		if (!(newpassword.equals(verifyNewPassword))) {
			this.getErrorMessages().add(MessageFactory.getMessage(MallConstants.USER_BUNDLE_NAME,"new.password.notmatch.error.message", FacesMessage.SEVERITY_ERROR).getDetail());
		}
		if (this.getErrorMessages().size() > 0) {
			return null;
		}
		if (oldPassword.equals(newpassword)) {
			this.getErrorMessages().add(MessageFactory.getMessage(MallConstants.USER_BUNDLE_NAME,"newandoldpassword.same.error.message", FacesMessage.SEVERITY_ERROR).getDetail());
			return null;
		}
		customer.setLoginPassword(this.getNewPassword());
		customerService.updateCustomerLoginPassword(customer);
		//刷新customer
		CustomerUtil.refreshCustomer();
		// set blank data
		this.setOldPassword("");
		this.setNewPassword("");
		this.setVerifyNewPassword("");
		this.getErrorMessages().add(MessageFactory.getMessage(MallConstants.USER_BUNDLE_NAME,"loginpassword.update.success.message", FacesMessage.SEVERITY_ERROR).getDetail());
		return null;
	}
}
